# -*- coding: utf-8 -*-
import os 
import time 
 
from os.path import join, getsize 
from heapq import nlargest 
 
import requests

import json

import re #写正则表达式要导入的

#发送网络请求
defaultPage = 0;
defaultKey = '';

def searchkeys(keyValue):
    
    arrKeys = keyValue.split('/')
    questionKey = ''
    for index in range(len(arrKeys)):
    
        if arrKeys[index] == 'question':
            questionKey = arrKeys[index + 1];
            break;
    return questionKey

def netWork(questionKey):
    urlValue = 'https://www.zhihu.com/api/v4/questions/{key}/feeds?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Creaction_instruction%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cvip_info%2Cbadge%5B*%5D.topics%3Bdata%5B*%5D.settings.table_of_content.enabled&offset={page}&limit=5&order=updated'.format(key=questionKey,page=defaultPage)
    response=requests.get(urlValue)
    '''
    print('-----------------')
    print('响应状态码：',response.status_code)
    print('-----------------')
    print('请求的网络地址为：',response.url)
    print('-----------------')
    print('头部信息为：',response.headers)
    print('-----------------')
    print('cookies信息为：',response.cookies)
    print('-----------------')
    '''
    #以文本形式打印网页源码
    response.encoding='utf-8'           #进行utf-8编码，避免乱码
    
    resDic = json.loads(response.content)
    arrData = resDic['data']


    if arrData:
        for item in arrData:
            if item != None:
                # abc = item['target']['excerpt'];
                # print(abc.encode('raw_unicode_escape'))
                # print(abc)
                re_img = r'<[img|IMG].*?>' 
                re_img_rc = r'.+?src="(\S+)"' 
                img_url_list = []
                img_list= re.findall(re_img, item['target']['content'])#找到所有的img标签
                for tag in img_list:
                    #print(tag)
                    img_url_list.append(re.findall(re_img_rc, tag)[0])#找到所有的img_url
                writeZhiHu({
                    'title': item['target']['excerpt'],
                    'content':item['target']['content'],
                    'imgs': img_url_list,
                })

    else:
        print(response.content)

# def exitFile():
    # os.path.exists()
    # os.path.join(path,oldname)



def writeZhiHu(dicValue):
    fileName = 'zhihuNew.json'

    print(os.getcwd())
    filePath = os.path.join(os.getcwd(), fileName)
    print(filePath)
    if os.path.exists(filePath):
        print('write file......')
        with open(filePath,'r',encoding = 'utf-8') as fp:
            data = json.load(fp)
            print(data) 
            data.append(dicValue)
            print(data)
            f = open(filePath,"w+")
            f.write(json.dumps(data)) #写入文件，设置为空
            f.close() #执行完结束
            
    else:
        f = open(filePath,"a+")
        f.write(json.dumps([dicValue])) #写入文件，设置为空
        f.close() #执行完结束
        
    time.sleep(12);
    defaultPage += 1;
    print('第' + str(defaultPage) + '页')
    netWork(defaultKey)
    
keyValue = 'https://www.zhihu.com/question/616371835/answer/3160853728'
key = searchkeys(keyValue)
defaultKey = key;
print("key" + key)
netWork(key)

# https://www.zhihu.com/question/617343564
'''
https://www.zhihu.com/api/v4/questions/608906016/feeds?include=data%5B*%5D.is_normal%2Cadmin_closed_comment%2Creward_info%2Cis_collapsed%2Cannotation_action%2Cannotation_detail%2Ccollapse_reason%2Cis_sticky%2Ccollapsed_by%2Csuggest_edit%2Ccomment_count%2Ccan_comment%2Ccontent%2Ceditable_content%2Cattachment%2Cvoteup_count%2Creshipment_settings%2Ccomment_permission%2Ccreated_time%2Cupdated_time%2Creview_info%2Crelevant_info%2Cquestion%2Cexcerpt%2Cis_labeled%2Cpaid_info%2Cpaid_info_content%2Creaction_instruction%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B*%5D.mark_infos%5B*%5D.url%3Bdata%5B*%5D.author.follower_count%2Cvip_info%2Cbadge%5B*%5D.topics%3Bdata%5B*%5D.settings.table_of_content.enabled&offset=0&limit=5&order=updated
'''
'''
{
  "type": "question_feed_card",
  "target_type": "answer",
  "target": {
    "admin_closed_comment": false,
    "annotation_action": null,
    "answer_type": "normal",
    "attached_info": "ogEQCAQQAxivwrbfCyCg1qyiApICJQoJNjAzMzQxNjMwEgozMTUyOTEyNjg3GAQiCklNQUdFX1RFWFQ=",
    "author": {
      "avatar_url": "https://picx.zhimg.com/v2-d341e3a4075eceae759779449ca78d5a_l.jpg?source=1940ef5c",
      "avatar_url_template": "https://pica.zhimg.com/v2-d341e3a4075eceae759779449ca78d5a.jpg?source=1940ef5c",
      "badge": [],
      "badge_v2": {
        "detail_badges": [],
        "icon": "",
        "merged_badges": [],
        "night_icon": "",
        "title": ""
      },
      "exposed_medal": {
        "avatar_url": "https://picx.zhimg.com/v2-ae0ef9685f799322052458ed4481c176_l.png?source=1940ef5c",
        "description": "回答 2 个问题",
        "medal_avatar_frame": "",
        "medal_id": "972440607604559872",
        "medal_name": "知无不言",
        "mini_avatar_url": "https://pica.zhimg.com/v2-ae0ef9685f799322052458ed4481c176_r.png?source=1940ef5c"
      },
      "follower_count": 0,
      "gender": -1,
      "headline": "",
      "id": "4d8c3b30bf881a21fb8293802329117d",
      "is_advertiser": false,
      "is_followed": false,
      "is_following": false,
      "is_org": false,
      "is_privacy": false,
      "name": "我是真的",
      "type": "people",
      "url": "https://www.zhihu.com/api/v4/people/4d8c3b30bf881a21fb8293802329117d",
      "url_token": "wo-shi-zhen-de-76-97",
      "user_type": "people"
    },
    "can_comment": {
      "reason": "",
      "status": true
    },
    "collapse_reason": "",
    "collapsed_by": "nobody",
    "comment_count": 0,
    "comment_permission": "all",
    "content": "<p data-pid=\"p_FsHpHx\">个人觉得是这张吧</p><p data-pid=\"uyV4-zAd\">在云南拍的</p><figure data-size=\"normal\"><noscript><img src=\"https://pic1.zhimg.com/50/v2-96e94fb361cd6ab6cfcc54780f436ad8_720w.jpg?source=1940ef5c\" data-rawwidth=\"4000\" data-rawheight=\"6000\" data-size=\"normal\" data-original-token=\"v2-1441d8ef2196fce07d6b8cf1e1e11623\" data-default-watermark-src=\"https://pic1.zhimg.com/50/v2-2753da310ef9a2ae46dabad9b1b562d5_720w.jpg?source=1940ef5c\" class=\"origin_image zh-lightbox-thumb\" width=\"4000\" data-original=\"https://picx.zhimg.com/v2-96e94fb361cd6ab6cfcc54780f436ad8_r.jpg?source=1940ef5c\"/></noscript><img src=\"data:image/svg+xml;utf8,&lt;svg xmlns=&#39;http://www.w3.org/2000/svg&#39; width=&#39;4000&#39; height=&#39;6000&#39;&gt;&lt;/svg&gt;\" data-rawwidth=\"4000\" data-rawheight=\"6000\" data-size=\"normal\" data-original-token=\"v2-1441d8ef2196fce07d6b8cf1e1e11623\" data-default-watermark-src=\"https://pic1.zhimg.com/50/v2-2753da310ef9a2ae46dabad9b1b562d5_720w.jpg?source=1940ef5c\" class=\"origin_image zh-lightbox-thumb lazy\" width=\"4000\" data-original=\"https://picx.zhimg.com/v2-96e94fb361cd6ab6cfcc54780f436ad8_r.jpg?source=1940ef5c\" data-actualsrc=\"https://pic1.zhimg.com/50/v2-96e94fb361cd6ab6cfcc54780f436ad8_720w.jpg?source=1940ef5c\"/></figure><p></p>",
    "content_mark": {},
    "created_time": 1691315003,
    "decorative_labels": [],
    "editable_content": "",
    "excerpt": "个人觉得是这张吧 在云南拍的 [图片]",
    "extras": "",
    "id": 3152912687,
    "is_collapsed": false,
    "is_copyable": true,
    "is_labeled": false,
    "is_mine": false,
    "is_normal": true,
    "is_sticky": false,
    "is_visible": true,
    "mark_infos": [],
    "question": {
      "created": 1687832351,
      "id": 608906016,
      "question_type": "normal",
      "relationship": {},
      "title": "能看看你相册里封神的照片吗？",
      "type": "question",
      "updated_time": 1687855812,
      "url": "https://www.zhihu.com/api/v4/questions/608906016"
    },
    "reaction_instruction": {},
    "relationship": {
      "is_author": false,
      "is_authorized": false,
      "is_nothelp": false,
      "is_thanked": false,
      "upvoted_followees": [],
      "voting": 0
    },
    "relevant_info": {
      "is_relevant": false,
      "relevant_text": "",
      "relevant_type": ""
    },
    "reshipment_settings": "allowed",
    "reward_info": {
      "can_open_reward": false,
      "is_rewardable": false,
      "reward_member_count": 0,
      "reward_total_money": 0,
      "tagline": ""
    },
    "settings": {
      "table_of_contents": {
        "enabled": false
      }
    },
    "sticky_info": "",
    "suggest_edit": {
      "reason": "",
      "status": false,
      "tip": "",
      "title": "",
      "unnormal_details": {
        "description": "",
        "note": "",
        "reason": "",
        "reason_id": 0,
        "status": ""
      },
      "url": ""
    },
    "thanks_count": 0,
    "thumbnail_info": {
      "count": 1,
      "thumbnails": [
        {
          "height": 6000,
          "token": "v2-1441d8ef2196fce07d6b8cf1e1e11623",
          "type": "image",
          "url": "https://pic1.zhimg.com/80/v2-1441d8ef2196fce07d6b8cf1e1e11623_720w.jpg?source=1940ef5c",
          "width": 4000
        }
      ],
      "type": "thumbnail_info"
    },
    "type": "answer",
    "updated_time": 1691315003,
    "url": "https://www.zhihu.com/api/v4/answers/3152912687",
    "visible_only_to_author": false,
    "voteup_count": 0,
    "zhi_plus_extra_info": ""
  },
  "skip_count": false,
  "position": 0,
  "cursor": "1b9ec550722aa622f1af3e200dbeed26"
}
'''
